package poly_array;
//多态数组
public class PolyArray {
    public static void main(String[] args) {
//        Person tom = new Person("tom", 10);
//        Student s1 = new Student("小张", 10, 70);
//        Student s2 = new Student("小王", 11,80);
//        Teacher t1 = new Teacher("张三", 30, 8000);
//        Teacher t2 = new Teacher("李四", 30,7000);
//        Person[] p1 = {s1,s2,t1,t2};
//        for(int i = 0; i < p1.length; i++){
//            System.out.println( p1[i].say());;
//        }

        //写法2
        Person[] person = new Person[5];
        person[0] = new Person("mary", 20);
        person[1] = new Student("jack", 18, 100);
        person[2] = new Student("smith", 10, 70);
        person[3] = new Teacher("king", 50, 20000);
        person[4] = new Teacher("tom", 30, 15000);

        for (int i = 0; i < 5; i++) {
            //编译类型Person,运行类型Student/Teacher(根据实际情况，由JVM机来判断)
            System.out.println(person[i].say());;//动态绑定机制

            //调用子类特有的方法:将编译类型改变,强转
            //首先判断运行类型是不是Student
            if(person[i] instanceof Student){
//                Student stu1 = (Student) person[i];
//                stu1.study();
                ((Student)person[i]).study();
            }else if(person[i] instanceof Teacher){
//                Teacher tea1 = (Teacher) person[i];
//                tea1.teach();
                ((Teacher)person[i]).teach();
            }else if(person[i] instanceof Person){
                ;
            }else{
                System.out.println("你输入的类型有误，请重新输入");
            }
        }
    }
}
